package com.demo.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.demo.bean.BeanBase;
import com.demo.common.Constants;
import com.demo.dao.BaseDAO;
import com.demo.service.EditBaseService;

@Service
public class EditBaseServiceImpl implements EditBaseService
{
	public Log logger = LogFactory.getLog(getClass());
	protected BaseDAO baseDAO;
	
	public int editBean(BeanBase beanBase)
	{
		int code = Constants.SUCCESS_ACTION;
		
		boolean flag = false;
		if(baseDAO != null && beanBase != null)
		{
			if(beanBase.getAction() == Constants.ADD_ACTION)
			{
				flag = checkBeanExist(beanBase);
				if(flag)
				{
					code = Constants.EXIST_BEAN_ACTION;
				}
				else
				{
					flag = baseDAO.saveObj(beanBase);
				}
			}
			else if(beanBase.getAction() == Constants.UPDATE_ACTION)
			{
				flag = checkBeanExistForUpdate(beanBase);
				if(flag)
				{
					code = Constants.EXIST_BEAN_ACTION;
				}
				else
				{
					flag = baseDAO.update(beanBase);
				}
			}
			else if(beanBase.getAction() == Constants.DELETE_ACTION)
			{
				flag = baseDAO.delete(beanBase); 
			}
		}
		
		if(!flag)
		{
			code = Constants.FAIL_ACTION;
		}
		
		return code;
	}
	
	public boolean checkBeanExist(BeanBase beanBase)
	{
		return false;
	}
	
	public boolean checkBeanExistForUpdate(BeanBase beanBase)
	{
		return false;
	}
	
	public void init(){}
	public void destroy(){}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	
}
